/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.view.util;

import com.reportgear.report.model.ReportModel;
import com.reportgear.report.model.auxiliary.ColumnPropList;
import com.reportgear.report.model.auxiliary.ColumnRow;
import com.reportgear.report.model.auxiliary.RowPropList;
import com.reportgear.report.view.JWorksheet;

/**
 * 工作薄使用类
 * 
 * @version 1.0 2011-3-20
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class WorksheetUtils {
	/**
	 * 
	 * 根据X、Y坐标获取ColumnRow
	 * 
	 * @param worksheet
	 *            工作簿
	 * @param x
	 *            x 坐标
	 * @param y
	 *            y 坐标
	 * @return
	 */
	public static ColumnRow getColumnRowByXY(JWorksheet worksheet, double x, double y) {
		ReportModel reportModel = worksheet.getReportModel();

		RowPropList rpl = reportModel.getRowPropList();
		ColumnPropList cpl = reportModel.getColumnPropList();

		double tmpValue = 0.0D;

		// 获取X索引
		int xx = worksheet.getHoriBeginValue();
		while (true) {
			tmpValue += cpl.getValue(xx);
			if (tmpValue > x) {
				break;
			}
			xx++;
		}

		// 计算Y索引
		tmpValue = 0.0D;
		int yy = worksheet.getVertBeginValue();
		while (true) {
			tmpValue += rpl.getValue(yy);
			if (tmpValue > y) {
				break;
			}
			yy++;
		}

		return ColumnRow.valueOf(xx, yy);
	}

	/**
	 * 根据 x，y位置在Sheet中查找单元格
	 * 
	 * @param workSheet
	 *            工作薄
	 * @param x
	 *            x位置
	 * @param y
	 *            y位置
	 * @return ColumnRow对象
	 */
	public static ColumnRow getAdjustEventColumnRow(JWorksheet workSheet, double x, double y) {
		ColumnRow columnRow = getColumnRowByXY(workSheet, x, y);
		int column = Math.max(columnRow.getColumn(), 0);
		int row = Math.max(columnRow.getRow(), 0);

		return ColumnRow.valueOf(column, row);
	}
}
